//
//  WQCRepaymentViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/22.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCRepaymentViewController.h"
#import "configuration.h"
#import "WQCRepaymoneyTableViewCell.h"
#import "WQCDetailViewController.h"
#import "WQCAddViewController.h"
#import "WQCAddViewController.h"
#import "WYCBillModel.h"
#import "WQCHuanTableViewCell.h"
@interface WQCRepaymentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *repaymentTableView;
@property(nonatomic,strong)NSMutableArray * billarray;
@property(nonatomic,strong) NSString  *bankName;

@end

@implementation WQCRepaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.repaymentTableView.mj_header beginRefreshing];

    self.billarray = [[NSMutableArray alloc] init];

    self.navigationController.navigationBarHidden =NO;
    
    self.view.backgroundColor = WYCGlobalBg;
    [self setupNav];
    self.repaymentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, kScreenW, kScreenH) style:UITableViewStyleGrouped];
    self.repaymentTableView.dataSource = self;
    self.repaymentTableView.delegate = self;
    self.repaymentTableView.sectionFooterHeight = 10;
    self.repaymentTableView.sectionHeaderHeight = 10;
    self.repaymentTableView.backgroundColor = [UIColor clearColor];
    self.repaymentTableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.repaymentTableView];
    [self loadBnaklist];

}
- (void)setupNav {
    
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"智能还款";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"add" hight:@"add" target:self action:@selector(addbankClick)];

}
#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.billarray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString * strID = @"cell";
    
    WQCHuanTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WQCHuanTableViewCell" owner:self options:nil]lastObject];
        
    }
    WYCBillModel * model = self.billarray[indexPath.row];

    cell.billLb.text = model.billDate;
    cell.borowLb.text = model.dueDate;
    cell.amount.text = model.creditLine;
    cell.nameLb.text =[NSString stringWithFormat:@"%@(%@)",[WYCAccountTool unarchiveuserName],model.lastNumber];
    
    NSString  *state = model.state;
    cell.typeLb.textColor = wycMain;
    NSLog(@"是什么%@",state);
    if ([state isEqualToString:@"0"]) {
      cell.typeLb.text =@"制定计划";
    }
    else if ([state isEqualToString:@"1"])
    {
           cell.typeLb.text =@"执行中";

    }
    else if ([state isEqualToString:@"2"])
        {
            cell.typeLb.text =@"执行成功";

        }
   else if ([state isEqualToString:@"3"])
    {
        cell.typeLb.text =@"执行中";
        
    } if ([state isEqualToString:@"4"])
    {
        cell.typeLb.text =@"执行失败";
        cell.typeLb.textColor = [UIColor redColor];
        
    }
    
    cell.bankBig.frame =CGRectMake(5, 5, ScreenWidth-10, 150);
    NSInteger bankid = [model.bankId integerValue];
    switch (bankid) {
        case 10000://招商银行
            cell.bankName.text =@"招商银行";
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_zhaoshang"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zs"];
            break;
        case 10001://工商银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_gs"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            cell.bankName.text =@"工商银行";

            break;
        case 10002://农业银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_ny"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_ny"];
            cell.bankName.text =@"农业银行";

            break;
        case 10003://  建设银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_jianshe"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_js"];
            cell.bankName.text =@"建设银行";

            break;
        case 10004://中国银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_zhongguo"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zg"];
            cell.bankName.text =@"中国银行";

            break;
        case 10005:  //浦发银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_pufa"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_pf"];
            cell.bankName.text =@"浦发银行";

            break;
        case 10006:  //交通银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_jiaotong"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_jt"];
            cell.bankName.text =@"交通银行";

            break;
        case 10007:  //民生银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_minsheng"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_ms"];
            cell.bankName.text =@"民生银行";

            break;
        case 10009:  //广东发展银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_gf"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gf"];
            cell.bankName.text =@"广发银行";

            break;
        case 10010:  //中信银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_zhongxin"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_zx"];
            cell.bankName.text =@"中信银行";

            break;
        case 10011:  //  华夏银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_hx"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_hx"];
            cell.bankName.text =@"华夏银行";

            break;
        case 10012:  //  兴业银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_xingye"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_xy"];
            cell.bankName.text =@"兴业银行";

            break;
        case 10014:  //  江苏银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_jiangsu"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            cell.bankName.text =@"江苏银行";

            break;
        case 10016:  //  北京银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_bj"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_bj"];
            cell.bankName.text =@"北京银行";

            break;
        case 10020://10020  光大银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_gd"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gd"];
            cell.bankName.text =@"光大银行";

            break;
        case  10024://平安银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_pinan"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_pa"];
            cell.bankName.text =@"平安银行";

            break;
        case 10027:  //上海银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_shanghai"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_sh"];
            cell.bankName.text =@"上海银行";

            break;
        case  10028://  中国邮政储蓄银行
            cell.bankBig.image = [UIImage imageNamed:@"bank_bg_youzheng"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_yz"];
            cell.bankName.text =@"邮政银行";

            break;
            
        default:
            cell.bankBig.image = [UIImage imageNamed:@"icon_other_bank"];
            cell.bankSmall.image = [UIImage imageNamed:@"bank_icon_gs"];
            
            break;
    }
    
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ WQCHuanTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       if (cell)
         {
             
                    _bankName = cell.bankName.text;
         }
    
    WYCBillModel * model = self.billarray[indexPath.row];
   WQCDetailViewController  *detailView= [[WQCDetailViewController alloc]init];
    detailView.billStr =model.billDate;
    detailView.borowStr = model.dueDate;
    detailView.amountStr = model.creditLine;
    detailView.lastnumberStr =model.lastNumber;
    detailView.bankIdStr = model.bankId;
    detailView.cardidStr = model.cardId;
    detailView.stateStr = model.state;
    detailView.cardNumberStr = model.cardNumber;
    detailView.mobileStr = model.mobile;
    detailView.validityStr = model.validity;
    detailView.cvn2Str = model.cvn2;
    detailView.dueDateStr =model.dueDate;
    detailView.nameStr = _bankName;
    
   [self.navigationController pushViewController:detailView animated:YES];
}
-(void)addbankClick
{
    NSString  *auth = [WYCAccountTool unarchiveisAuth];
    if ([auth isEqualToString:@"1"]) {
        [SVProgressHUD showImage:nil status:@"您还没有认证，请先实名认证！"];
        return;
    }
    else
    {
        WQCAddViewController  *addView = [[WQCAddViewController alloc]init];
        [self.navigationController pushViewController:addView animated:YES];
    }
  
}
-(void)loadBnaklist
{
    [self.billarray removeAllObjects];
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditCardList,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"返回%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{

                    [self.billarray addObject:model];
                    
                    if (self.billarray.count == 0)
                    {
//                        self.tableView.hidden = YES;
//                        [self ObtainButton];
                    }else{
//                        self.tableView.hidden = NO;
//                        [self.billView setHidden:YES];
                    }
                }
            }
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        
        }
        
        [self.repaymentTableView reloadData];
        [self.repaymentTableView.mj_header endRefreshing];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
       
    }];
}
@end
